package com.syp.auth.export.support.client;

import java.net.URI;

import org.apache.http.client.HttpClient;
import org.jboss.resteasy.client.ClientExecutor;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.springframework.beans.factory.InitializingBean;

import com.syp.auth.domain.ProcessResult;
import com.syp.auth.domain.vo.AppVo;
import com.syp.auth.export.common.GeneralUserVo;
import com.syp.auth.export.request.AppLogRequest;
import com.syp.auth.export.request.TokenVerifyRequest;
import com.syp.auth.export.resource.AuthorizeResourceI;
import com.syp.auth.export.response.AppLogResponse;
import com.syp.auth.export.response.TokenVerifyResponse;

@SuppressWarnings("deprecation")
public class DefaultAuthorizeResourceClientProxy implements AuthorizeResourceI, InitializingBean {

	private URI baseUri;
	private AuthorizeResourceI client;
	private HttpClient httpClient;
	private ClientExecutor clientExecutor;
	private ResteasyProviderFactory resteasyProviderFactory;

	@Override
	public TokenVerifyResponse verify(TokenVerifyRequest request) {
		return client.verify(request);
	}

	@Override
	public ProcessResult signout(String sid) {
		return client.signout(sid);
	}

	@Override
	public GeneralUserVo cuser(String sid) {
		return client.cuser(sid);
	}	

	@Override
	public AppVo capp(String app) {
		return client.capp(app);
	}

	@Override
	public AppLogResponse log(AppLogRequest request) {
		return client.log(request);
	}
	
	

	@Override
	public void afterPropertiesSet() throws Exception {
		if (resteasyProviderFactory == null)
			resteasyProviderFactory = ResteasyProviderFactory.getInstance();
		RegisterBuiltin.register(resteasyProviderFactory);

		if (clientExecutor == null) {
			if (httpClient == null) {
				clientExecutor = new ApacheHttpClient4Executor();
			} else {
				clientExecutor = new ApacheHttpClient4Executor(httpClient);
			}
			client = ProxyFactory.create(AuthorizeResourceI.class, baseUri, clientExecutor, resteasyProviderFactory);
		} else
			client = ProxyFactory.create(AuthorizeResourceI.class, baseUri, clientExecutor, resteasyProviderFactory);
	}

	public URI getBaseUri() {
		return baseUri;
	}

	public void setBaseUri(URI baseUri) {
		this.baseUri = baseUri;
	}

	public AuthorizeResourceI getClient() {
		return client;
	}

	public void setClient(AuthorizeResourceI client) {
		this.client = client;
	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public ResteasyProviderFactory getResteasyProviderFactory() {
		return resteasyProviderFactory;
	}

	public void setResteasyProviderFactory(ResteasyProviderFactory resteasyProviderFactory) {
		this.resteasyProviderFactory = resteasyProviderFactory;
	}

}
